package com.dhwl.hsh.merchant.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dhwl.hsh.lib.bean.resp.CarTypeResp;
import com.dhwl.hsh.merchant.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 简要描述
 *
 * @author Derek.Kidd
 * @version V1.0.0
 * @date 2016-04-28 17:48
 */
public class TruckListAdapter extends BaseAdapter {


    int checkNum = 0;

    public TruckListAdapter(Context context, List list) {
        super(context, list);
    }


//
//    public TruckListAdapter(Context context, List<CarTypeResp> carTypeRespList) {
//        super(context);
////        iconTruck = context.getResources().obtainTypedArray(R.array.truck_icon);
////        titleArr = context.getResources().getStringArray(R.array.truck_title);
////        infoArr = context.getResources().getStringArray(R.array.truck_info);
//    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_truck_list, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        CarTypeResp carTypeResp = (CarTypeResp) getList().get(position);

        if (URLUtil.isNetworkUrl(carTypeResp.getCartypeImg())) {
            Glide.with(context).load(carTypeResp.getCartypeImg()).into(viewHolder.iconLeftTruck);
        }

        viewHolder.tvTruckTitle.setText(carTypeResp.getCartypeName());


//        <item>载重：1吨\n长宽高：1.7*1.2*1m\n超公里费：3元/公里</item>

        String str = "载重：" + carTypeResp.getCartypeLoad() + "\n长宽高：" + carTypeResp.getCartypeSize() + "\n超公里费：" + carTypeResp.getCartypeOverPrice();
        viewHolder.tvTruckInfo.setText(str);

        if (checkNum == position) {
            viewHolder.layoutTruck.setPressed(true);
            viewHolder.iconLeftTruck.setPressed(true);
            viewHolder.checkBoxItemTruck.setVisibility(View.VISIBLE);
        } else {
            viewHolder.layoutTruck.setPressed(false);
            viewHolder.iconLeftTruck.setPressed(false);
            viewHolder.checkBoxItemTruck.setVisibility(View.INVISIBLE);
        }


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkNum = position;
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public int getCheckNum() {
        return checkNum;
    }

    static class ViewHolder {
        @Bind(R.id.icon_left_truck)
        ImageView iconLeftTruck;
        @Bind(R.id.layout_truck)
        RelativeLayout layoutTruck;
        @Bind(R.id.tv_truck_title)
        TextView tvTruckTitle;
        @Bind(R.id.tv_truck_info)
        TextView tvTruckInfo;
        @Bind(R.id.checkBox_item_truck)
        CheckBox checkBoxItemTruck;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
